*This program generates common household attributes for NSS Schedule 1
* using the household-level and household roster (person-level) data

***Set this to the data directory
local rootdir 
cd "`rootdir'"





****38th round******NEED TO GET PERSON DATA FROM SCHEDULE 10 HERE! The only case where it is necessary

***There must be some kind of scrambled region code or something,

use "\Round38\round38_chh", clear

local hhid sector region subround subsample fsu hhno

bysort `hhid': gen counter=_N
duplicates drop `hhid', force
save "\Round38\temp", replace

use "round38\d156pr.dta", clear
append using "round38\d156pu.dta"
gen aservant=0
replace aservant=1 if rel_to_hd==9
bysort `hhid': egen servant=max(aservant)
gen region=statereg38
ren hhreligion religion_alt
ren hhtypecode htype_alt
ren hhgroup hgroup_alt
bysort `hhid': gen hhsize_alt=_N
keep `hhid' rel_to_hd sex age marital gen_edu *_alt servant
merge m:1 `hhid' using "round38\temp", update replace

***For now only drop the employment data for guys that weren't matched
***this means it is missing for some households
*about 3,000 out of 117,000 households in the household file
tab _merge
drop if _merge==1
drop _merge

drop if hhsize==0
drop *_alt hhconsistent

bysort `hhid': gen hhsize_den=_N


foreach j in a s b c p m y{
gen `j'male=0
gen `j'female=0
}

*adults
replace amale=1 if sex==1 & age>17 & age<60
replace afemale=1 if sex==2 & age>17 & age<60
bysort `hhid': egen numamale=total(amale)
bysort `hhid': egen numafemale=total(afemale)

*seniors
replace smale=1 if sex==1 & age>=60 & age<.
replace sfemale=1 if sex==2 & age>=60 & age<.
bysort `hhid': egen numsmale=total(smale)
bysort `hhid': egen numsfemale=total(sfemale)


*babies
replace bmale=1 if sex==1 & age<=2
replace bfemale=1 if sex==2 & age<=2
bysort `hhid': egen numbmale=total(bmale)
bysort `hhid': egen numbfemale=total(bfemale)

*children 3-4
replace cmale=1 if sex==1 & age>2 & age<5
replace cfemale=1 if sex==2 & age>2 & age<5
bysort `hhid': egen numcmale=total(cmale)
bysort `hhid': egen numcfemale=total(cfemale)

* 5-9 --primary school age
replace pmale=1 if sex==1 & age>4 & age<10
replace pfemale=1 if sex==2 & age>4 & age<10
bysort `hhid': egen numpmale=total(pmale)
bysort `hhid': egen numpfemale=total(pfemale)

*10-14 --middle school age
replace mmale=1 if sex==1 & age>9 & age<15
replace mfemale=1 if sex==2 & age>9 & age<15
bysort `hhid': egen nummmale=total(mmale)
bysort `hhid': egen nummfemale=total(mfemale)

*15-17 --youths
replace ymale=1 if sex==1 & age>14 & age<18
replace yfemale=1 if sex==2 & age>14 & age<18
bysort `hhid': egen numymale=total(ymale)
bysort `hhid': egen numyfemale=total(yfemale)


foreach j in a s b c p m y{
gen dmratio`j'=num`j'male/hhsize_den
gen dfratio`j'=num`j'female/hhsize_den
replace dmratio`j'=0 if dmratio`j'==.
replace dfratio`j'=0 if dfratio`j'==.
}



***Generate education shares
***education conversion - do we want years of schooling for children?
gen generaledmod=gen_edu
replace generaledmod=6 if generaledmod==9 | generaledmod==7 | generaledmod==8
replace generaledmod=0 if generaledmod==10
replace generaledmod=. if (age<18 | age==.) | sex==.

*0 - not literate
*1 - literate but no formal schooling
*2 - literate but below primary
*3 - primary
*4 - middle
*5 - secondary
*6 - university


replace generaledmod=. if age<18
replace generaledmod=0 if generaledmod==. & age>17


***Fraction of male, female working age adults with education levels 0-6
forvalues j=0(1)6{

bysort `hhid': egen num_ed`j'=total(afemale+sfemale+smale+amale) if generaledmod==`j'
bysort `hhid': egen edratio`j'=min(num_ed`j')

replace edratio`j'=edratio`j'/hhsize_den
replace edratio`j'=0 if edratio`j'==.
}




gen yschooling=.
*middle school grades 6-8
replace yschooling=0 if generaledmod==0
replace yschooling=0 if generaledmod==1
replace yschooling=0 if generaledmod==1
replace yschooling=(11-5)/2 if generaledmod==2
replace yschooling=(11-5) if generaledmod==3
replace yschooling=(14-5) if generaledmod==4
replace yschooling=(18-5) if generaledmod==5
replace yschooling=(22-5) if generaledmod==6
***mean years of adult schooling
bysort `hhid': egen meana_school=mean(yschooling)




***Household head
gen head=0
replace head=1 if rel_to_hd==1
gen female=0
replace female=1 if sex==2
bysort `hhid': egen headed=max(head*generaledmod)
bysort `hhid': egen headfemale=max(head*female)
bysort `hhid': egen headage=max(head*age)
drop head female


***hh type/group/religion conversion
*note that there are only two household types in round 38 - self-employed and other!   in all later rounds 9 gets split into 2(regular salary) 3(casual labor) 9(other)
replace hgroup=9 if hgroup==0
replace hgroup=9 if hgroup==3


***nic/nco conversions
**not sure what to do with negatives, set to missing for now-- maybe read the missing "-" as negative?
replace nic=. if nic<0
replace nco=. if nco<0



tostring nic, gen(hh_nic70) format(%03.0f)
gen nic70=substr(hh_nic70,1,2)
sort nic70
merge m:1 nic70 using "`rootdir'nic70_87_2digit"
drop nic70 _merge
ren nic87 nic87mult
merge m:1 nic87mult using "`rootdir'nic87_87_2digit"
replace nic87mult=nic87 if _merge==3
drop nic87 _merge
ren nic87mult hh_nic


tab hh_nic

*convert to 1-digit categories:
gen nic1=substr(hh_nic,1,1)

tostring nco, gen(hh_nco) format(%03.0f)
gen nco1=substr(hh_nco,1,1)
drop nic nco



***Land possessed -- just one variable, units conversion necessary?
gen ximprent=rent if sector==2
replace ximprent=0 if ximprent==.
gen xactrent=rent if sector==1 & ownhland==2
replace xactrent=rent if sector==2 & ownhland==2

*convert land to hectares
replace land=land*0.404686
replace land=0 if land<=0
replace land=99.99 if land>99.99 & land~=.

***housing/cooking [imputed rent?]-- conversions
ren floortype typefloor
***alternate mpce


***Meals - two square, meals to others, number of days people are here.
gen mealsothers=guestcermeals+guestsmeals+empmeals

duplicates drop `hhid', force

**multiplier
gen mult=multcomb


cap: gen segment=1
cap: gen substratum=1

# delimit ;

keep `hhid'
round segment substratum
hhsize hhsize_den
dmratio* dfratio*
edratio*  meana_school
headed headfemale headage
hgroup religion htype
hh_nic hh_nco hh_nic70 nic1 nco1
mpce ximprent xactrent
land
mult 
cook light 
ownhland covarea plinth typedwell typestruct typefloor condition
mealsothers fammealfree fammealpay fammealhome
latrine water twosq servant;
save round38_hh, replace;

# delimit cr


***********************************
*************43rd round************
use "Round43\round43_chh", clear
*merge variables
local hhid sector region subround subsample fsu substratum hhno
ren landpos land
duplicates drop `hhid', force
save "Round43\temp", replace

use "Round43\round43_cpers", clear
bysort `hhid': gen hhsize_alt=_N
merge m:1 `hhid' using "Round43\temp", update replace
drop if _merge==1
drop _merge



drop if hhsize==0
gen hhconsistent=0
replace hhconsistent=1 if hhsize==hhsize_alt & hhsize_alt~=. & hhsize~=.
tab hhconsistent



***Demographics

bysort `hhid': gen hhsize_den=_N

foreach j in a s b c p m y{
gen `j'male=0
gen `j'female=0
}

*adults
replace amale=1 if sex==1 & age>17 & age<60
replace afemale=1 if sex==2 & age>17 & age<60
bysort `hhid': egen numamale=total(amale)
bysort `hhid': egen numafemale=total(afemale)

*seniors
replace smale=1 if sex==1 & age>=60 & age<.
replace sfemale=1 if sex==2 & age>=60 & age<.
bysort `hhid': egen numsmale=total(smale)
bysort `hhid': egen numsfemale=total(sfemale)


*babies
replace bmale=1 if sex==1 & age<=2
replace bfemale=1 if sex==2 & age<=2
bysort `hhid': egen numbmale=total(bmale)
bysort `hhid': egen numbfemale=total(bfemale)

*children 3-4
replace cmale=1 if sex==1 & age>2 & age<5
replace cfemale=1 if sex==2 & age>2 & age<5
bysort `hhid': egen numcmale=total(cmale)
bysort `hhid': egen numcfemale=total(cfemale)

* 5-9 --primary school age
replace pmale=1 if sex==1 & age>4 & age<10
replace pfemale=1 if sex==2 & age>4 & age<10
bysort `hhid': egen numpmale=total(pmale)
bysort `hhid': egen numpfemale=total(pfemale)

*10-14 --middle school age
replace mmale=1 if sex==1 & age>9 & age<15
replace mfemale=1 if sex==2 & age>9 & age<15
bysort `hhid': egen nummmale=total(mmale)
bysort `hhid': egen nummfemale=total(mfemale)

*15-17 --youths
replace ymale=1 if sex==1 & age>14 & age<18
replace yfemale=1 if sex==2 & age>14 & age<18
bysort `hhid': egen numymale=total(ymale)
bysort `hhid': egen numyfemale=total(yfemale)


foreach j in a s b c p m y{
gen dmratio`j'=num`j'male/hhsize_den
gen dfratio`j'=num`j'female/hhsize_den
replace dmratio`j'=0 if dmratio`j'==.
replace dfratio`j'=0 if dfratio`j'==.
}




***Generate education shares
***education conversion - do we want years of schooling for children?
gen generaledmod=genedcode
replace generaledmod=6 if generaledmod==9 | generaledmod==7 | generaledmod==8
replace generaledmod=0 if generaledmod==10
replace generaledmod=. if (age<18 | age==.) | sex==.

*0 - not literate
*1 - literate but no formal schooling
*2 - literate but below primary
*3 - primary
*4 - middle
*5 - secondary
*6 - university


replace generaledmod=. if age<18
replace generaledmod=0 if generaledmod==. & age>17


***Fraction of male, female working age adults with education levels 0-6
forvalues j=0(1)6{
bysort `hhid': egen num_ed`j'=total(afemale+sfemale+smale+amale) if generaledmod==`j'
bysort `hhid': egen edratio`j'=min(num_ed`j')
replace edratio`j'=edratio`j'/hhsize_den
replace edratio`j'=0 if edratio`j'==.
}




gen yschooling=.
*middle school grades 6-8
replace yschooling=0 if generaledmod==0
replace yschooling=0 if generaledmod==1
replace yschooling=0 if generaledmod==1
replace yschooling=(11-5)/2 if generaledmod==2
replace yschooling=(11-5) if generaledmod==3
replace yschooling=(14-5) if generaledmod==4
replace yschooling=(18-5) if generaledmod==5
replace yschooling=(22-5) if generaledmod==6
***mean years of adult schooling
bysort `hhid': egen meana_school=mean(yschooling)




***Household head
gen head=0
replace head=1 if relhh==1
gen female=0
replace female=1 if sex==2
bysort `hhid': egen headed=max(head*generaledmod)
bysort `hhid': egen headfemale=max(head*female)
bysort `hhid': egen headage=max(head*age)
drop head female


***hh type/group/religion conversion
*note that there are only two household types in round 38 - self-employed and other!   in all later rounds 9 gets split into 2(regular salary) 3(casual labor) 9(other)
*replace hgroup=9 if hgroup==0
*replace hgroup=9 if hgroup==3

***nic/nco conversions

**not sure what to do with negatives, set to missing for now
replace nic=. if nic<0
replace nco=. if nco<0


tostring nic, gen(hh_nic70) format(%03.0f)
gen nic70=substr(hh_nic70,1,2)
sort nic70
merge m:1 nic70 using "`rootdir'nic70_87_2digit"
drop nic70 _merge
ren nic87 nic87mult
merge m:1 nic87mult using "`rootdir'nic87_87_2digit"
replace nic87mult=nic87 if _merge==3
drop nic87 _merge
ren nic87mult hh_nic


*convert to 1-digit categories:
gen nic1=substr(hh_nic,1,1)

tostring nco, gen(hh_nco) format(%03.0f)
gen nco1=substr(hh_nco,1,1)
drop nic nco




***Land possessed -- just one variable, units conversion necessary?
replace land=0 if land<=0
replace land=99.99 if land>99.99 & land~=.

***housing/cooking [imputed rent?]-- conversions
gen ximprent=rent if sector==2
gen xactrent=rent if sector==1 & ownhouse==2
replace xactrent=rent if sector==2 & ownhouse==2


***Meals - two square, meals to others, number of days people are here.
gen mealsothers=0
forvalues j=1(1)7{
replace cer`j'meals=0 if cer`j'meals==.
replace mealsothers=mealsothers+cer`j'meals
}


foreach j in daysaway meals mealsfree mealspay mealshome{
replace `j'=0 if `j'==.
bysort `hhid': egen tot`j'=total(`j')
}
ren totdaysaway famdaysaway
ren totmeals fammeals
ren totmealsfree fammealfree
ren totmealspay fammealpay
ren totmealshome fammealhome


ren ownhouse ownhland
ren coverarea covarea

*this includes non-servants...
gen aservant=0
replace aservant=1 if relhh==9
bysort `hhid': egen servant=max(aservant)

duplicates drop `hhid', force

gen pds_shop=ration

cap: gen segment=1

# delimit ;

keep `hhid'
round district segment
hhsize hhsize_den
dmratio* dfratio*
edratio* meana_school
headed headfemale headage
hgroup religion htype
hh_nic hh_nco hh_nic70 nic1 nco1
mpce ximprent xactrent
land
mult 
cook light 
ownhland covarea plinth typedwell typestruct typefloor condition
mealsothers fammealfree fammealpay fammealhome famdaysaway fammeals
pds_shop
publicworks irdp servant

;
save round43_hh, replace;

# delimit cr












****************************************
*******************55th round************


use "Round55\round55_chh", clear
*merge variables
local hhid sector region subround subsample fsu substratum segment hhno
ren landpos land
ren mult_ss mult
replace mult=mult/800
ren irop irdp
duplicates drop `hhid', force
save "Round55\temp", replace

use "Round55\round55_cpers", clear
bysort `hhid': gen hhsize_alt=_N
merge m:1 `hhid' using "Round55\temp", update replace
drop if _merge==1
drop _merge



drop if hhsize==0
gen hhconsistent=0
replace hhconsistent=1 if hhsize==hhsize_alt & hhsize_alt~=. & hhsize~=.
tab hhconsistent

***Demographics
bysort `hhid': gen hhsize_den=_N


foreach j in a s b c p m y{
gen `j'male=0
gen `j'female=0
}

*adults
replace amale=1 if sex==1 & age>17 & age<60
replace afemale=1 if sex==2 & age>17 & age<60
bysort `hhid': egen numamale=total(amale)
bysort `hhid': egen numafemale=total(afemale)

*seniors
replace smale=1 if sex==1 & age>=60 & age<.
replace sfemale=1 if sex==2 & age>=60 & age<.
bysort `hhid': egen numsmale=total(smale)
bysort `hhid': egen numsfemale=total(sfemale)


*babies
replace bmale=1 if sex==1 & age<=2
replace bfemale=1 if sex==2 & age<=2
bysort `hhid': egen numbmale=total(bmale)
bysort `hhid': egen numbfemale=total(bfemale)

*children 3-4
replace cmale=1 if sex==1 & age>2 & age<5
replace cfemale=1 if sex==2 & age>2 & age<5
bysort `hhid': egen numcmale=total(cmale)
bysort `hhid': egen numcfemale=total(cfemale)

* 5-9 --primary school age
replace pmale=1 if sex==1 & age>4 & age<10
replace pfemale=1 if sex==2 & age>4 & age<10
bysort `hhid': egen numpmale=total(pmale)
bysort `hhid': egen numpfemale=total(pfemale)

*10-14 --middle school age
replace mmale=1 if sex==1 & age>9 & age<15
replace mfemale=1 if sex==2 & age>9 & age<15
bysort `hhid': egen nummmale=total(mmale)
bysort `hhid': egen nummfemale=total(mfemale)

*15-17 --youths
replace ymale=1 if sex==1 & age>14 & age<18
replace yfemale=1 if sex==2 & age>14 & age<18
bysort `hhid': egen numymale=total(ymale)
bysort `hhid': egen numyfemale=total(yfemale)


foreach j in a s b c p m y{
gen dmratio`j'=num`j'male/hhsize_den
gen dfratio`j'=num`j'female/hhsize_den
replace dmratio`j'=0 if dmratio`j'==.
replace dfratio`j'=0 if dfratio`j'==.
}



***Generate education shares
***education conversion - do we want years of schooling for children?
gen generaledmod=generaled

replace generaledmod=0 if generaledmod==1
replace generaledmod=1 if generaledmod==2 | generaledmod==3 | generaledmod==4
replace generaledmod=2 if generaledmod==5
replace generaledmod=3 if generaledmod==6
replace generaledmod=4 if generaledmod==7
replace generaledmod=5 if generaledmod==8 | generaled==9
replace generaledmod=6 if generaledmod>9 & generaledmod~=.
replace generaledmod=. if (age<18 | age==.) | sex==.
*0 - not literate
*1 - literate but no formal schooling
*2 - literate but below primary
*3 - primary
*4 - middle
*5 - secondary
*6 - university


replace generaledmod=. if age<18
replace generaledmod=0 if generaledmod==. & age>17

***Fraction of male, female working age adults with education levels 0-6
forvalues j=0(1)6{
bysort `hhid': egen num_ed`j'=total(afemale+sfemale+smale+amale) if generaledmod==`j'
bysort `hhid': egen edratio`j'=min(num_ed`j')

replace edratio`j'=edratio`j'/hhsize_den
replace edratio`j'=0 if edratio`j'==.
}





gen yschooling=.
*middle school grades 6-8
replace yschooling=0 if generaledmod==0
replace yschooling=0 if generaledmod==1
replace yschooling=0 if generaledmod==1
replace yschooling=(11-5)/2 if generaledmod==2
replace yschooling=(11-5) if generaledmod==3
replace yschooling=(14-5) if generaledmod==4
replace yschooling=(18-5) if generaledmod==5
replace yschooling=(22-5) if generaledmod==6
***mean years of adult schooling
bysort `hhid': egen meana_school=mean(yschooling)




***Household head
gen head=0
replace head=1 if relhh==1
gen female=0
replace female=1 if sex==2
bysort `hhid': egen headed=max(head*generaledmod)
bysort `hhid': egen headfemale=max(head*female)
bysort `hhid': egen headage=max(head*age)
drop head female


***hh type/group/religion conversion
*note that there are only two household types in round 38 - self-employed and other!   in all later rounds 9 gets split into 2(regular salary) 3(casual labor) 9(other)
*replace hgroup=9 if hgroup==0
*replace hgroup=9 if hgroup==3


***nic/nco conversions

**not sure what to do with negatives, set to missing for now
replace nic=. if nic<0
replace nco=. if nco<0

*convert to 1-digit categories:

tostring nic, gen(hh_nic98) format(%05.0f)
tostring nco, gen(hh_nco) format(%03.0f)
drop nic nco

foreach j in hh_nic98{
gen temp`j'=""
gen nic98=substr(`j',1,4)
merge m:1 nic98 using "`rootdir'nic98_87"
replace temp`j'=nic87 if _merge==3
drop _merge nic98 nic87
gen nic98=substr(`j',1,3)
merge m:1 nic98 using "`rootdir'nic98_87"
replace temp`j'=nic87 if _merge==3
drop _merge nic98 nic87
gen nic98=substr(`j',1,2)
merge m:1 nic98 using "`rootdir'nic98_87"
replace temp`j'=nic87 if _merge==3
drop _merge nic98 nic87
gen hh_nic=substr(temp`j',1,2)
}

gen nic1=substr(hh_nic,1,1)
gen nco1=substr(hh_nco,1,1)





***Land possessed -- just one variable, units conversion necessary?
replace land=0 if land<=0

replace land=land/100
replace land=99.99 if land>99.99 & land~=.




foreach j in daysaway mealsday mealsfree mealsemp mealsother mealspay mealshome{
replace `j'=0 if `j'==.
bysort `hhid': egen tot`j'=total(`j')
}

ren totdaysaway famdaysaway
gen fammeals=totmealsday
gen fammealfree=totmealsfree+totmealsemp+totmealsother
ren totmealspay fammealpay
ren totmealshome fammealhome

replace cook=9 if cook==0
replace cook=9 if cook==10
replace cook=9 if cook==.


replace light=9 if light==6
replace light=9 if light==7
replace light=9 if light==.

*ren coverarea covarea
*ren homestead ownhland
*gen plinth=.

*this includes non-servants...
gen aservant=0
replace aservant=1 if relhh==9
bysort `hhid': egen servant=max(aservant)


duplicates drop `hhid', force


replace round=55

# delimit ;

keep `hhid'
round district
hhsize hhsize_den
dmratio* dfratio*
edratio* meana_school
headed headfemale headage
hgroup religion htype
hh_nic98 hh_nic hh_nco nic1 nco1
mpce 
land
mult 
cook light 
fammealfree fammealpay fammealhome famdaysaway fammeals
twosq 
irdp publicworks servant;
*particular to this round but maybe of interest cunit totfoodexp totnonfoodexp;
save round55_hh, replace;

# delimit cr






*************************************************
************************61st round****************

use "round61\round61_chh", clear
*merge variables
local hhid sector region subround subsample fsu substratum segment hhno
ren landpos land
ren mpce365 mpce
replace mpce=mpce/100
ren enoughfood twosq
replace mult=mult/200

ren ben1 food_for_work
ren ben2 annapoorna
ren ben3 icds
ren ben4 midday_meal

duplicates drop `hhid', force
save "round61\temp", replace

egen totpop=total(hhsize*mult)
summ totpop

use "round61\round61_cpers", clear
bysort `hhid': gen hhsize_alt=_N
merge m:1 `hhid' using "round61\temp", update replace
drop if _merge==1
drop _merge



egen totpop=total(mult*hhsize)
summ totpop

drop if hhsize==0
gen hhconsistent=0
replace hhconsistent=1 if hhsize==hhsize_alt & hhsize_alt~=. & hhsize~=.
tab hhconsistent

bysort `hhid': gen hhsize_den=_N

foreach j in a s b c p m y{
gen `j'male=0
gen `j'female=0
}

*adults
replace amale=1 if sex==1 & age>17 & age<60
replace afemale=1 if sex==2 & age>17 & age<60
bysort `hhid': egen numamale=total(amale)
bysort `hhid': egen numafemale=total(afemale)

*seniors
replace smale=1 if sex==1 & age>=60 & age<.
replace sfemale=1 if sex==2 & age>=60 & age<.
bysort `hhid': egen numsmale=total(smale)
bysort `hhid': egen numsfemale=total(sfemale)


*babies
replace bmale=1 if sex==1 & age<=2
replace bfemale=1 if sex==2 & age<=2
bysort `hhid': egen numbmale=total(bmale)
bysort `hhid': egen numbfemale=total(bfemale)

*children 3-4
replace cmale=1 if sex==1 & age>2 & age<5
replace cfemale=1 if sex==2 & age>2 & age<5
bysort `hhid': egen numcmale=total(cmale)
bysort `hhid': egen numcfemale=total(cfemale)

* 5-9 --primary school age
replace pmale=1 if sex==1 & age>4 & age<10
replace pfemale=1 if sex==2 & age>4 & age<10
bysort `hhid': egen numpmale=total(pmale)
bysort `hhid': egen numpfemale=total(pfemale)

*10-14 --middle school age
replace mmale=1 if sex==1 & age>9 & age<15
replace mfemale=1 if sex==2 & age>9 & age<15
bysort `hhid': egen nummmale=total(mmale)
bysort `hhid': egen nummfemale=total(mfemale)

*15-17 --youths
replace ymale=1 if sex==1 & age>14 & age<18
replace yfemale=1 if sex==2 & age>14 & age<18
bysort `hhid': egen numymale=total(ymale)
bysort `hhid': egen numyfemale=total(yfemale)


foreach j in a s b c p m y{
gen dmratio`j'=num`j'male/hhsize_den
gen dfratio`j'=num`j'female/hhsize_den
replace dmratio`j'=0 if dmratio`j'==.
replace dfratio`j'=0 if dfratio`j'==.
}




***Generate education shares
***education conversion - do we want years of schooling for children?
gen generaledmod=generaled

replace generaledmod=0 if generaledmod==1
replace generaledmod=1 if generaledmod==2
replace generaledmod=2 if generaledmod==3
replace generaledmod=3 if generaledmod==4
replace generaledmod=4 if generaledmod==5
replace generaledmod=5 if generaledmod==6 | generaled==7
replace generaledmod=6 if generaledmod>7 & generaledmod~=.
replace generaledmod=. if (age<18 | age==.) | sex==.
*0 - not literate
*1 - literate but no formal schooling
*2 - literate but below primary
*3 - primary
*4 - middle
*5 - secondary
*6 - university



replace generaledmod=. if age<18
replace generaledmod=0 if generaledmod==. & age>17


***Fraction of male, female working age adults with education levels 0-6
forvalues j=0(1)6{
bysort `hhid': egen num_ed`j'=total(afemale+sfemale+smale+amale) if generaledmod==`j'
bysort `hhid': egen edratio`j'=min(num_ed`j')

replace edratio`j'=edratio`j'/hhsize_den
replace edratio`j'=0 if edratio`j'==.

}



gen yschooling=.
*middle school grades 6-8
replace yschooling=0 if generaledmod==0
replace yschooling=0 if generaledmod==1
replace yschooling=0 if generaledmod==1
replace yschooling=(11-5)/2 if generaledmod==2
replace yschooling=(11-5) if generaledmod==3
replace yschooling=(14-5) if generaledmod==4
replace yschooling=(18-5) if generaledmod==5
replace yschooling=(22-5) if generaledmod==6
***mean years of adult schooling
bysort `hhid': egen meana_school=mean(yschooling)




***Household head
gen head=0
replace head=1 if relhh==1
gen female=0
replace female=1 if sex==2
bysort `hhid': egen headed=max(head*generaledmod)
bysort `hhid': egen headfemale=max(head*female)
bysort `hhid': egen headage=max(head*age)
drop head female


***hh type/group/religion conversion
*note that there are only two household types in round 38 - self-employed and other!   in all later rounds 9 gets split into 2(regular salary) 3(casual labor) 9(other)
*replace hgroup=9 if hgroup==0
*replace hgroup=9 if hgroup==3



***nic/nco conversions

**not sure what to do with negatives, set to missing for now
replace nic=. if nic<0
replace nco=. if nco<0

*convert to 1-digit categories:



tostring nic, gen(hh_nic98) format(%05.0f)
tostring nco, gen(hh_nco) format(%03.0f)
drop nic nco

foreach j in hh_nic98{
gen temp`j'=""
gen nic98=substr(`j',1,4)
merge m:1 nic98 using "`rootdir'nic98_87"
replace temp`j'=nic87 if _merge==3
drop _merge nic98 nic87
gen nic98=substr(`j',1,3)
merge m:1 nic98 using "`rootdir'nic98_87"
replace temp`j'=nic87 if _merge==3
drop _merge nic98 nic87
gen nic98=substr(`j',1,2)
merge m:1 nic98 using "`rootdir'nic98_87"
replace temp`j'=nic87 if _merge==3
drop _merge nic98 nic87
gen hh_nic=substr(temp`j',1,2)
}

gen nic1=substr(hh_nic,1,1)
gen nco1=substr(hh_nco,1,1)




***Land possessed -- just one variable, units conversion necessary?
replace land=0 if land<=0
replace land=land/1000
replace land=99.99 if land>99.99 & land~=.


***Meals - two square, meals to others, number of days people are here.
gen mealsothers=mealsguests
replace mealsothers=0 if mealsothers==.


foreach j in daysaway mealsday mealsfree mealsemp mealsother mealspay mealshome{
replace `j'=0 if `j'==.
bysort `hhid': egen tot`j'=total(`j')
}

ren totdaysaway famdaysaway
gen fammeals=totmealsday
gen fammealfree=totmealsfree+totmealsemp+totmealsother
ren totmealspay fammealpay
ren totmealshome fammealhome


replace cook=9 if cook==0
replace cook=9 if cook==10
replace cook=9 if cook==.

replace light=9 if light==6
replace light=9 if light==.


*this includes non-servants...
gen aservant=0
replace aservant=1 if relhh==9
bysort `hhid': egen servant=max(aservant)


duplicates drop `hhid', force

**multiplier

gen pds_card=ration
gen pds_type=typeration

ren dwelling typedwell



# delimit ;

keep `hhid'
round district
hhsize hhsize_den
dmratio* dfratio*
edratio* meana_school
headed headfemale headage
hgroup religion htype
hh_nic98 hh_nic hh_nco nic1 nco1
mpce 
land
mult 
cook light 
typedwell
mealsothers fammealfree fammealpay fammealhome famdaysaway fammeals
twosq pds_card pds_type
food_for_work
annapoorna
icds
midday_meal servant
;
save round61_hh, replace;

# delimit cr





 
 
*************************************************
***** ************66th round*********************
 ****Note that there are now two types of households, depending on the reporting period for consumption
 *****Type 1 is the 30 day reporting period for everything


 
************Type 1********

use "round66\round661_chh", clear
*merge variables
ren landpos land

gen match=1
replace match=2 if nss~=nsc
replace mult=mult/(match*100)
replace mpce=mpce/100

local hhid sector region subround subsample fsu substratum segment hhno
duplicates drop `hhid', force
save "round66\temp", replace

use "round66\round661_cpers", clear
bysort `hhid': gen hhsize_alt=_N
merge m:1 `hhid' using "round66\temp", update replace
drop if _merge==1
drop _merge



drop if hhsize==0
gen hhconsistent=0
replace hhconsistent=1 if hhsize==hhsize_alt & hhsize_alt~=. & hhsize~=.
tab hhconsistent


bysort `hhid': gen hhsize_den=_N


foreach j in a s b c p m y{
gen `j'male=0
gen `j'female=0
}

*adults
replace amale=1 if sex==1 & age>17 & age<60
replace afemale=1 if sex==2 & age>17 & age<60
bysort `hhid': egen numamale=total(amale)
bysort `hhid': egen numafemale=total(afemale)

*seniors
replace smale=1 if sex==1 & age>=60 & age<.
replace sfemale=1 if sex==2 & age>=60 & age<.
bysort `hhid': egen numsmale=total(smale)
bysort `hhid': egen numsfemale=total(sfemale)


*babies
replace bmale=1 if sex==1 & age<=2
replace bfemale=1 if sex==2 & age<=2
bysort `hhid': egen numbmale=total(bmale)
bysort `hhid': egen numbfemale=total(bfemale)

*children 3-4
replace cmale=1 if sex==1 & age>2 & age<5
replace cfemale=1 if sex==2 & age>2 & age<5
bysort `hhid': egen numcmale=total(cmale)
bysort `hhid': egen numcfemale=total(cfemale)

* 5-9 --primary school age
replace pmale=1 if sex==1 & age>4 & age<10
replace pfemale=1 if sex==2 & age>4 & age<10
bysort `hhid': egen numpmale=total(pmale)
bysort `hhid': egen numpfemale=total(pfemale)

*10-14 --middle school age
replace mmale=1 if sex==1 & age>9 & age<15
replace mfemale=1 if sex==2 & age>9 & age<15
bysort `hhid': egen nummmale=total(mmale)
bysort `hhid': egen nummfemale=total(mfemale)

*15-17 --youths
replace ymale=1 if sex==1 & age>14 & age<18
replace yfemale=1 if sex==2 & age>14 & age<18
bysort `hhid': egen numymale=total(ymale)
bysort `hhid': egen numyfemale=total(yfemale)


foreach j in a s b c p m y{
gen dmratio`j'=num`j'male/hhsize_den
gen dfratio`j'=num`j'female/hhsize_den
replace dmratio`j'=0 if dmratio`j'==.
replace dfratio`j'=0 if dfratio`j'==.
}



***Generate education shares
***education conversion - do we want years of schooling for children?
gen generaledmod=generaled

*gen generaledmod=gen_edu
replace generaledmod=0 if generaledmod==1
replace generaledmod=1 if generaledmod==2 | generaledmod==3 | generaledmod==4
replace generaledmod=2 if generaledmod==5
replace generaledmod=3 if generaledmod==6
replace generaledmod=4 if generaledmod==7
replace generaledmod=5 if generaledmod==8 | generaledmod==10
replace generaledmod=6 if generaledmod>10 & generaledmod~=.
replace generaledmod=. if age<18 | age==. | sex==.
*0 - not literate
*1 - literate but no formal schooling
*2 - literate but below primary
*3 - primary
*4 - middle
*5 - secondary
*6 - university


replace generaledmod=. if age<18
replace generaledmod=0 if generaledmod==. & age>17


***Fraction of male, female working age adults with education levels 0-6
forvalues j=0(1)6{
bysort `hhid': egen num_ed`j'=total(afemale+sfemale+smale+amale) if generaledmod==`j'
bysort `hhid': egen edratio`j'=min(num_ed`j')

replace edratio`j'=edratio`j'/hhsize_den
replace edratio`j'=0 if edratio`j'==.
}




gen yschooling=.
*middle school grades 6-8
replace yschooling=0 if generaledmod==0
replace yschooling=0 if generaledmod==1
replace yschooling=0 if generaledmod==1
replace yschooling=(11-5)/2 if generaledmod==2
replace yschooling=(11-5) if generaledmod==3
replace yschooling=(14-5) if generaledmod==4
replace yschooling=(18-5) if generaledmod==5
replace yschooling=(22-5) if generaledmod==6
***mean years of adult schooling
bysort `hhid': egen meana_school=mean(yschooling)




***Household head
gen head=0
replace head=1 if relhh==1
gen female=0
replace female=1 if sex==2
bysort `hhid': egen headed=max(head*generaledmod)
bysort `hhid': egen headfemale=max(head*female)
bysort `hhid': egen headage=max(head*age)
drop head female


***hh type/group/religion conversion
*note that there are only two household types in round 38 - self-employed and other!   in all later rounds 9 gets split into 2(regular salary) 3(casual labor) 9(other)
*replace hgroup=9 if hgroup==0
*replace hgroup=9 if hgroup==3


***nic/nco conversions

**not sure what to do with negatives, set to missing for now
replace nic=. if nic<0
replace nco=. if nco<0

*convert to 1-digit categories:
tostring nic, gen(hh_nic04) format(%05.0f)
tostring nco, gen(hh_nco04) format(%03.0f)
drop nic nco



*first convert nic04 to nic98
foreach j in hh_nic04{
gen nic04=substr(`j',1,4)
merge m:1 nic04 using "`rootdir'nic04_98"
gen hh_nic98=nic98
drop nic98 nic04 _merge
}
*then convert to 2-digit nic87
foreach j in hh_nic98{
gen temp`j'=""
gen nic98=substr(`j',1,4)
merge m:1 nic98 using "`rootdir'nic98_87"
replace temp`j'=nic87 if _merge==3
drop _merge nic98 nic87
gen nic98=substr(`j',1,3)
merge m:1 nic98 using "`rootdir'nic98_87"
replace temp`j'=nic87 if _merge==3
drop _merge nic98 nic87
gen nic98=substr(`j',1,2)
merge m:1 nic98 using "`rootdir'nic98_87"
replace temp`j'=nic87 if _merge==3
drop _merge nic98 nic87
drop `j'
gen hh_nic=substr(temp`j',1,2)
*drop temp`j'
}

gen nic1=substr(hh_nic,1,1)


***note that this match is far from perfect, keep original
foreach j in hh_nco04{
gen nco04=`j'
merge m:1 nco04 using "`rootdir'nco_cross"
drop nco04 _merge
ren nco68 hh_nco
}


gen nco1=substr(hh_nco,1,1)





***Land possessed -- just one variable, units conversion necessary?
replace land=0 if land<=0
replace land=land/1000
replace land=99.99 if land>99.99 & land~=.


***Meals - two square, meals to others, number of days people are here.
gen mealsothers=mealsguests
replace mealsothers=0 if mealsothers==.


foreach j in daysaway mealsday mealsfree mealsemp mealsother mealspay mealshome{
replace `j'=0 if `j'==.
bysort `hhid': egen tot`j'=total(`j')
}

ren totdaysaway famdaysaway
gen fammeals=totmealsday
gen fammealfree=totmealsfree+totmealsemp+totmealsother
ren totmealspay fammealpay
ren totmealshome fammealhome



replace cook=9 if cook==0
replace cook=9 if cook==10
replace cook=9 if cook==.

replace light=9 if light==6
replace light=9 if light==.

*this includes non-servants...
gen aservant=0
replace aservant=1 if relhh==9
bysort `hhid': egen servant=max(aservant)

duplicates drop `hhid', force

**multiplier

ren dwelling typedwell

ren enoughfood twosq


# delimit ;

keep `hhid'
round district
hhsize hhsize_den
dmratio* dfratio*
edratio* meana_school
headed headfemale headage
hgroup religion htype
hh_nic hh_nco hh_nic04 hh_nco04 nic1 nco1
mpce 
land
mult 
cook light 
typedwell
mealsothers fammealfree fammealpay fammealhome famdaysaway fammeals
twosq servant;
save round66_hh, replace;

